package com.zzyl;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.core.RedisTemplate;


@SpringBootTest
public class RedisTest {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Test
    public void test2(){
        redisTemplate.boundHashOps("user").put("name","zzyl");
        redisTemplate.opsForHash().put("user","age","18");
        Object name = redisTemplate.opsForHash().get("user", "name");
        System.out.println(name);
    }

    @Test
    public void test() {
        redisTemplate.opsForValue().set("name", "张三");
        redisTemplate.opsForValue().set("age", "18");
        String s = redisTemplate.opsForValue().get("name");
        String age = redisTemplate.opsForValue().get("age");
        System.out.println(age);
        System.out.println(s);
    }

    @Test
    public void test1(){
        redisTemplate.boundHashOps("user").put("name","张三");
        redisTemplate.opsForHash().put("user","age","18");
        Object name =redisTemplate.opsForHash().get("user","name");
        System.out.println(name);
//        System.out.println(redisTemplate.opsForValue().get("name"));
    }
    /**
     * 操作字符串类型的数据
     */
//    @Test
//    public void testString() {
//        // 普通的set方法
//        redisTemplate.opsForValue().set("name", "张三");
//        System.out.println(redisTemplate.opsForValue().get("name"));
//        // 设置带有过期时间的key
//        redisTemplate.opsForValue().set("token", "123qweasd",20, TimeUnit.SECONDS);
//        System.out.println(redisTemplate.opsForValue().get("token"));
//
//        // setnx 当指定的键key不存在时,会将key的值设置为value，返回true，否则返回false(不能覆盖原有值)
//        System.out.println(redisTemplate.opsForValue().setIfAbsent("lock", "09876", 5, TimeUnit.MINUTES));
//        System.out.println(redisTemplate.opsForValue().setIfAbsent("lock", "34567", 5, TimeUnit.MINUTES));
//    }

    }